package database.local;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import database.local.DataBaseClient.MyAsyncSyncronyser;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import app.ui.OpeningActivity;
import app.ui.PreferencesActivity;

public class OpeningWindowSyncronizer {
	private List<Business> business_list;
	private OpeningActivity context;
	
	public OpeningWindowSyncronizer(OpeningActivity context){
		this.context=context;
	}
	
	
	public List<Business> getBusiness_list() {
		return business_list;
	}


	public void setBusiness_list(List<Business> business_list) {
		this.business_list = business_list;
	}


	public class MyAsyncSyncronyser extends AsyncTask<String, Void, Integer> {
		protected Integer doInBackground(String... params) {
			Log.w("AndroApp", "Started sync with OpeningWindowSyncronyser");
			try {
				QueryData query = new QueryData();
				if (params.length > 0) {
					query.setCountry(params[0]);
					query.setCity(params[1]);
				}
				if (params.length > 2) {
					query.setType_btm(Integer.parseInt(params[2]));
					query.setDays_bitmap(Integer.valueOf(params[3]));
					Log.i("DataClient", "Time here is: " + params[4]);
					query.setTime(params[4]);
					query.setTimeDelta(Integer.parseInt(params[5]));
				}
				// query relevant deals
				business_list = query.getData();
				if (business_list == null)
					return 0;

				Log.w("AndroApp", "Finished sync with MyAsyncSyncronyser");
				return 1;
			} catch (IOException e1) {
				Log.e("DEBUGTAG", "Remote business list exception", e1);
			}

			Log.w("AndroApp", "Finished sync with MyAsyncSyncronyser");
			return 0;
		}

		protected void onPostExecute(Integer result) {
			Log.i("AndroApp",
					"Done getting data, found " + business_list.size()
							+ " business");
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			String like_str = prefs.getString(PreferencesActivity.PREF_LIKES, "");
			
			Log.i("AndroApp","Arranging Businesses");
			
			if (like_str!=null){
				Log.i("AndroApp","Likes String: " + like_str);
				
				List<String> likes = Arrays.asList(like_str.split("#"));
				
				List<Business> temp1 = new ArrayList<Business>();
				List<Business> temp2 = new ArrayList<Business>();
				for (Business b:business_list){
					if (likes.contains(String.valueOf( b.getDealMaxRate().getDeal_type()))){
						temp1.add(b);
					}
					else
					{
						temp2.add(b);
					}
				}
				business_list = temp1;
				business_list.addAll(temp2);
			
			context.setBusiness_list(business_list);
			context.flip.setVisibility(View.VISIBLE);
		}

	}
	}
	
	// Query the database. Optional inputs: Country, City
		// Data ("dd/mm/yy"), Time ("hh:mm"), Delta Time (in hours)
		// Example: myclass.Execute("Israel", "Tel-Aviv", "25/12/11", "20:00", "1")
		// Comments: Delta=1 means from Time to Time + 1
		public void sync(String... params) {
			MyAsyncSyncronyser inst = new MyAsyncSyncronyser();
			try {
				inst.execute(params);
			} catch (Exception e) {
				Log.e("DEBUGTAG", "Problem with sycronization", e);
			}
		}
	
	}
